namespace RushMail.Requests;

public class PoolAddress
{
    private readonly IRushMailClient _client;

    public PoolAddress(IRushMailClient client)
    {
        _client = client;
    }

    /// <summary>
    /// 获得地址池的页数
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&address-export-page
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<int>> ExportPage(int poolId) =>
        (await _client.PostAsync(new RushMailRequest("wc-pool", "address-export-page")
        {
            Body = new Dictionary<string, string>() {{"pool_id", poolId.ToString()}}
        })).ToGeneric<int>();

    /// <summary>
    /// 导出地址池
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&action=address-export
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<PoolAddressListResponse>> Export(int poolId, int page = 1) =>
        (await _client.PostAsync(new RushMailRequest("wc-pool", "address-export"){
            Body = new Dictionary<string, string>()
            {
                {"pool_id", poolId.ToString()},
                {"page", page.ToString()},
            }
        }))
        .ToGeneric<PoolAddressListResponse>();

    /// <summary>
    /// 添加地址池地址
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&action=address-add
    /// </summary>
    /// <returns></returns>
    public virtual async Task<RushMailResponse> Add(PoolAddressAdd body) =>
        (await _client.PostAsync(new RushMailRequest("wc-pool", "address-add")
        {
            Body = body.ToDictionary()
        }));
    
    /// <summary>
    /// 更新地址池的地址个数, 当添加完全部的地址后，请调用此接口重新计算地址池的地址个数
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&action=update-address-count
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse> UpdateAddressCount(int poolId) =>
        (await _client.PostAsync(new RushMailRequest("wc-pool", "update-address-count")
        {
            Body = new Dictionary<string, string>(){{"pool_id", poolId.ToString()}}
        }));

    
    /// <summary>
    ///  修改地址池
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&action=address-edit
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Edit(PoolAddressEdit body) =>
        _client.PostAsync(new RushMailRequest("wc-pool", "address-edit")
        {
            Body = body.ToDictionary()
        });
    
    /// <summary>
    /// 删除地址池
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&action=address-delete
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Delete(int poolId, IEnumerable<int> addrIdList) =>
        _client.PostAsync(new RushMailRequest("wc-pool", "address-delete")
        {
            Body = new Dictionary<string, string>()
            {
                {"pool_id", poolId.ToString()},
                {"addr_id_list", string.Join(',', addrIdList)},
            }
        });

    /// <summary>
    /// 从文件导入地址池地址
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&action=address-import
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Import(int poolId, Stream addrFile, string redirectUrl) =>
        throw new NotImplementedException();
}

public class PoolAddressListResponse
{
    public List<PoolAddressItemWithAddressId> Records { get; set; }
    public int Current { get; set; }
    [JsonProperty("is_next")]public bool IsNext { get; set; }
}

public class PoolAddressItem
{

    /// <summary>
    /// 邮箱地址
    /// </summary>
    public string Address { get; set; }
    
    
    /// <summary>
    /// 地址用户名
    /// </summary>
    public string Name { get; set; }

}

public class PoolAddressItemWithAddressId : PoolAddressItem
{
    /// <summary>
    /// 邮箱地址
    /// </summary>
    [JsonProperty("address_id")]public string AddressId { get; set; }
}

public class PoolAddressAdd
{
    /// <summary>
    /// 地址池 id
    /// </summary>
    [JsonProperty("pool_id")]public int PoolId { get; set; }
   
    /// <summary>
    /// 邮箱地址 1\t 名称\t 变量 1\n 邮箱地址 2\t 名称\t 变量 1 相当于一个地址的各值之间用制表符连接， 多个邮箱之间用换行符连接 单次调用添加的邮箱不得超过 100 个
    /// </summary>
    public List<PoolAddressItem> Data { get; set; }

    public virtual Dictionary<string, string> ToDictionary() => new Dictionary<string, string>()
    {
        {"pool_id", PoolId.ToString()},
        {"data", Data.Aggregate(new StringBuilder(), (sb, item) => sb.AppendLine($"{item.Address}\t{item.Name}")).ToString()},
    };
}

public class PoolAddressEdit
{
    /// <summary>
    /// 地址池 id
    /// </summary>
    [JsonProperty("pool_id")]public int PoolId { get; set; }
    [JsonProperty("address_id")]public int AddressId { get; set; }
    public string Name { get; set; }
    
    public virtual Dictionary<string, string> ToDictionary() => new Dictionary<string, string>()
    {
        {"pool_id", PoolId.ToString()},
        {"address_id", AddressId.ToString()},
        {"name", Name},
    };

}
